package lesson30;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyBIO {

    static volatile  boolean stop = false;


    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        int count = 0;
        try {
            ServerSocket serverSocket = new ServerSocket(9000);
            while (!stop){
                if(count == 10){
                    stop = true;
                }

                Socket socket = serverSocket.accept();

                executorService.submit(() ->{
                    try {
                        Scanner scanner = new Scanner(socket.getInputStream());
                        PrintStream printStream = new PrintStream(socket.getOutputStream());
                        while (!stop){
                            printStream.println("PONG : " + scanner.nextLine());
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });

                count ++;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
